/***************************************************************************//**
 * @file CGI/AircraftForce.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef AIRCRAFT_FORCE_CPP
#define AIRCRAFT_FORCE_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <osg/Geode>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osg/ShapeDrawable>

#include <CGI/AircraftForce.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

AircraftForce::AircraftForce( CGI_Data *data ) :
    m_data ( data )
{
    m_aircraftForceGroup = createAircraftForceGroup();
}

////////////////////////////////////////////////////////////////////////////////

void AircraftForce::update() {}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::Group> AircraftForce::createAircraftForceGroup()
{
    osg::ref_ptr<osg::Group> aircraftForceGroup = new osg::Group();
    aircraftForceGroup->setName( "AircraftForceGroup" );

    //////////////////////////////////////////////////////
    {
        osg::ref_ptr<osg::PositionAttitudeTransform> xConePAT = new osg::PositionAttitudeTransform();
        osg::ref_ptr<osg::PositionAttitudeTransform> yConePAT = new osg::PositionAttitudeTransform();
        osg::ref_ptr<osg::PositionAttitudeTransform> zConePAT = new osg::PositionAttitudeTransform();

        osg::ref_ptr<osg::Geode> xConeGeode = new osg::Geode();
        osg::ref_ptr<osg::Geode> yConeGeode = new osg::Geode();
        osg::ref_ptr<osg::Geode> zConeGeode = new osg::Geode();

        osg::ref_ptr<osg::Cone> xCone = new osg::Cone( osg::Vec3(), 1, 3 );
        osg::ref_ptr<osg::Cone> yCone = new osg::Cone( osg::Vec3(), 1, 3 );
        osg::ref_ptr<osg::Cone> zCone = new osg::Cone( osg::Vec3(), 1, 3 );

        osg::ref_ptr<osg::ShapeDrawable> xConeDrawable = new osg::ShapeDrawable( xCone.get() );
        osg::ref_ptr<osg::ShapeDrawable> yConeDrawable = new osg::ShapeDrawable( yCone.get() );
        osg::ref_ptr<osg::ShapeDrawable> zConeDrawable = new osg::ShapeDrawable( zCone.get() );

        xConeGeode->addDrawable( xConeDrawable );
        yConeGeode->addDrawable( yConeDrawable );
        zConeGeode->addDrawable( zConeDrawable );

        osg::ref_ptr<osg::StateSet> xConeStateSet = xConeGeode->getOrCreateStateSet();
        osg::ref_ptr<osg::StateSet> yConeStateSet = yConeGeode->getOrCreateStateSet();
        osg::ref_ptr<osg::StateSet> zConeStateSet = zConeGeode->getOrCreateStateSet();

        osg::ref_ptr<osg::Material> xConeMaterial = new osg::Material();
        osg::ref_ptr<osg::Material> yConeMaterial = new osg::Material();
        osg::ref_ptr<osg::Material> zConeMaterial = new osg::Material();

        xConeMaterial->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0f, 0.0f, 0.0f, 1.0f ) );
        yConeMaterial->setDiffuse( osg::Material::FRONT, osg::Vec4( 0.0f, 1.0f, 0.0f, 1.0f ) );
        zConeMaterial->setDiffuse( osg::Material::FRONT, osg::Vec4( 0.0f, 0.0f, 1.0f, 1.0f ) );

        xConeStateSet->setAttribute( xConeMaterial.get() );
        yConeStateSet->setAttribute( yConeMaterial.get() );
        zConeStateSet->setAttribute( zConeMaterial.get() );

        xConePAT->addChild( xConeGeode.get() );
        yConePAT->addChild( yConeGeode.get() );
        zConePAT->addChild( zConeGeode.get() );

        xConePAT->setPosition( osg::Vec3d( 20.0,  0.0,  0.0 ) );
        yConePAT->setPosition( osg::Vec3d(  0.0, 20.0,  0.0 ) );
        zConePAT->setPosition( osg::Vec3d(  0.0,  0.0, 20.0 ) );

        osg::Quat x_rotation;
        osg::Quat y_rotation;

        static const double pi_2 = 1.5707963267948966192313216916398;

        x_rotation.makeRotate(  pi_2, osg::Vec3d(0,1,0) );
        y_rotation.makeRotate( -pi_2, osg::Vec3d(1,0,0) );

        xConePAT->setAttitude( x_rotation );
        yConePAT->setAttitude( y_rotation );

        aircraftForceGroup->addChild( xConePAT.get() );
        aircraftForceGroup->addChild( yConePAT.get() );
        aircraftForceGroup->addChild( zConePAT.get() );
    }
    //////////////////////////////////////////////////////

    return aircraftForceGroup;
}
